﻿using System;
using System.Runtime.InteropServices;

namespace GMusicBox.Core
{
    public static class VTaskDialog
    {
        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
        private static extern IntPtr GetActiveWindow();

        [DllImport("comctl32.dll", CharSet = CharSet.Unicode)]
        private static extern int TaskDialog(IntPtr hWndParent, IntPtr hInstance, string pszWindowTitle, string pszMainInstruction, string pszContent, int dwCommonButtons, IntPtr pszIcon, out int pnButton);

        public static bool CanUseTankDialog()
        {
            return Environment.OSVersion.Version.Major >= 6;
        }

        [Flags]
        public enum TaskDialogButton
        {
            Cancel = 8,
            Close = 0x20,
            No = 4,
            OK = 1,
            Retry = 0x10,
            Yes = 2
        }

        public enum TaskDialogIcon : long
        {
            Information = 0xfffdL,
            None = 0L,
            SecurityError = 0xfff9L,
            SecurityShield = 0xfffcL,
            SecurityShieldBlue = 0xfffbL,
            SecurityShieldGray = 0xfff7L,
            SecuritySuccess = 0xfff8L,
            SecurityWarning = 0xfffaL,
            Stop = 0xfffeL,
            Warning = 0xffffL
        }

        public enum TaskDialogResult
        {
            None,
            OK,
            Cancel,
            Abort,
            Retry,
            Ignore,
            Yes,
            No,
            Close
        }

        public static TaskDialogResult ShowTaskDialog(string text)
        {
            int num;
            try
            {
                if (TaskDialog(GetActiveWindow(), IntPtr.Zero, null, null, text, (int)TaskDialogButton.OK, new IntPtr((long)TaskDialogIcon.Information), out num) != 0)
                    throw new Exception("调用对话框失败！");
            }
            catch (EntryPointNotFoundException exception)
            {
                throw new Exception("无法加载公共控件库6.0，您必须在Windows 6.0及更高版本中运行，而且需要程序有一个可用的manifest文件。", exception);
            }
            catch (Exception exception2)
            {
                throw new Exception("无法创建对话框！", exception2);
            }

            if ((num > 0) && (num <= 8))
                return (TaskDialogResult)num;
            return TaskDialogResult.None;
        }

        public static TaskDialogResult ShowTaskDialog(string text, string caption)
        {
            int num;
            try
            {
                if (TaskDialog(GetActiveWindow(), IntPtr.Zero, caption, null, text, (int)TaskDialogButton.OK, new IntPtr((long)TaskDialogIcon.Information), out num) != 0)
                    throw new Exception("调用对话框失败！");
            }
            catch (EntryPointNotFoundException exception)
            {
                throw new Exception("无法加载公共控件库6.0，您必须在Windows 6.0及更高版本中运行，而且需要程序有一个可用的manifest文件。", exception);
            }
            catch (Exception exception2)
            {
                throw new Exception("无法创建对话框！", exception2);
            }

            if ((num > 0) && (num <= 8))
                return (TaskDialogResult)num;
            return TaskDialogResult.None;
        }

        public static TaskDialogResult ShowTaskDialog(string text, string caption, string caption2)
        {
            int num;
            try
            {
                if (TaskDialog(GetActiveWindow(), IntPtr.Zero, caption, caption2, text, (int)TaskDialogButton.OK, new IntPtr((long)TaskDialogIcon.Information), out num) != 0)
                    throw new Exception("调用对话框失败！");
            }
            catch (EntryPointNotFoundException exception)
            {
                throw new Exception("无法加载公共控件库6.0，您必须在Windows 6.0及更高版本中运行，而且需要程序有一个可用的manifest文件。", exception);
            }
            catch (Exception exception2)
            {
                throw new Exception("无法创建对话框！", exception2);
            }

            if ((num > 0) && (num <= 8))
                return (TaskDialogResult)num;
            return TaskDialogResult.None;
        }

        public static TaskDialogResult ShowTaskDialog(string text, string caption, string caption2, TaskDialogButton buttons)
        {
            int num;
            try
            {
                if (TaskDialog(GetActiveWindow(), IntPtr.Zero, caption, caption2, text, (int)buttons, new IntPtr((long)TaskDialogIcon.Information), out num) != 0)
                    throw new Exception("调用对话框失败！");
            }
            catch (EntryPointNotFoundException exception)
            {
                throw new Exception("无法加载公共控件库6.0，您必须在Windows 6.0及更高版本中运行，而且需要程序有一个可用的manifest文件。", exception);
            }
            catch (Exception exception2)
            {
                throw new Exception("无法创建对话框！", exception2);
            }

            if ((num > 0) && (num <= 8))
                return (TaskDialogResult)num;
            return TaskDialogResult.None;
        }

        public static TaskDialogResult ShowTaskDialog(string text, string caption, string caption2, TaskDialogButton buttons, TaskDialogIcon icon)
        {
            int num;
            try
            {
                if (TaskDialog(GetActiveWindow(), IntPtr.Zero, caption, caption2, text, (int)buttons, new IntPtr((long)icon), out num) != 0)
                    throw new Exception("调用对话框失败！");
            }
            catch (EntryPointNotFoundException exception)
            {
                throw new Exception("无法加载公共控件库6.0，您必须在Windows 6.0及更高版本中运行，而且需要程序有一个可用的manifest文件。", exception);
            }
            catch (Exception exception2)
            {
                throw new Exception("无法创建对话框！", exception2);
            }

            if ((num > 0) && (num <= 8))
                return (TaskDialogResult)num;
            return TaskDialogResult.None;
        }
    }
}
